import { getList as commonBaseEquipment, rowDetail } from '@/api/baseequipment/commonBaseEquipment'

export const commonBaseEquipmentDocumentDialog = {
  data () {
    return {
      commonBaseEquipment, // 获取弹框数据接口
      commonBaseEquipmentDocumentDialogVisible: false, //表格弹框隐藏显示
      hasCommonBaseEquipmentDocumentFooter: true, // 是否需要底部按钮默认true
      commonBaseEquipmentDocumentSearchDialog: {
        inline: true,
        formDesc: {
          equipmentName: {
            type: 'input',
            label: $i18n.t('mesMaterialStationModel.equipmentName'),
            layout: 12
          },
          equipmentCode: {
            type: 'input',
            label: $i18n.t('mesMaterialStationModel.equipmentCode'),
            layout: 12
          }
        }
      },
      commonBaseEquipmentDocumentDialogColumn: [
        {
          type: 'selection'
        },
        {
          type: 'index',
          label: $i18n.t('common.order'),
          width: 60
        },
        {
          prop: 'equipmentName',
          label: $i18n.t('mesMaterialStationModel.equipmentName')
        },
        {
          prop: 'equipmentCode',
          label: $i18n.t('mesMaterialStationModel.equipmentCode')
        }
        //   {
        //     prop: "id",
        //     label: "id",
        //     visible:"fasle",
        //  //展示时字段转化
        //     style:"display: none",
        //   },
      ]
    }
  },
  methods: {
    // 弹框表格，双击表格，或者点击确定 返回选中数据
    getSelectCommonBaseEquipmentDocument (data) {
      this.formDataDialog = {}
      // 调试完成记得删除控制台打印
      console.log('选中数据', data)
      // 这里可以自定义返回字段信息
      // 第一个入参为当前表单绑定的对象，第二个入参为要修改的字段名，第三个入参为选中数据中相应信息
      this.$set(this.formDataDialog, 'equipmentCode', data[0].equipmentCode)
      this.$set(this.formDataDialog, 'equipmentName', data[0].equipmentName)
      rowDetail(data[0].id).then((res) => {
        if (res.data.result.areaCode) {
          this.$set(this.formDataDialog, 'areaCode', res.data.result.areaCode)
          this.$refs.dialogTable.initForm.modelingCode = res.data.result.areaCode
        }
        if (res.data.result.areaId) {
          this.$set(this.formDataDialog, 'areaId', res.data.result.areaId)
          const id = res.data.result.areaId
          this.mapTree(this.baseFactoryModelingTree, id)
        }
        this.$set(this.formDataDialog, 'detailList', res.data.result.detailList || [])
      })
    },
    mapTree (list, id) {
      let flag = true
      list.forEach((item) => {
        if (flag && id == item.id) {
          this.$set(this.formDataDialog, 'areaCode', item.modelingCode)
          this.$set(this.formDataDialog, 'areaName', item.modelingName)
          this.$set(this.formDataDialog, 'areaParentId', item.parentId)
          this.$set(this.formDataDialog, 'level', item.level)
          this.$refs.dialogTable.initForm.modelingCode = item.modelingCode
          flag = false
        }
        // 遍历子树
        if (flag && Array.isArray(item.children) && item.children.length) {
          this.mapTree(item.children, id)
        }
      })
    }
  }
}
